package com.gtw.modules.api.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.PageUtils;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbCommodityPropertyService;
import com.gtw.modules.api.service.TbPropertyService;
import com.gtw.modules.api.service.TbShoppingCartServices;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.*;
import com.gtw.modules.sys.service.TbOutGoosService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 代理提货货controller
 * Created by wuye on 2018/3/26.
 */
@RestController
@RequestMapping("/api/outgoos")
public class TbOutGoosController {

    @Autowired
    private TbOutGoosService tbOutGoosService;

    @Autowired
    private TbShoppingCartServices tbShoppingCartServices;

    @Autowired
    private TbPropertyService tbPropertyService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbCommodityPropertyService tbCommodityPropertyService;


    /**
     * 代理提货
     *
     * @return
     */
    @RequestMapping("/tihuo")
    public R tihuo(@RequestParam String addressid, @RequestParam String shoppingid, @RequestParam String remark, @RequestParam String idcard, @RequestParam String phone, @RequestParam String phone2, @LoginUserApi TbUser tbUser) {
        TbOutGoos tbOutGoos = new TbOutGoos();
        JSONArray ja = JSONArray.fromObject(shoppingid);
        float money = 0;
        for (int i = 0; i < ja.size(); i++) {
            TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(Integer.parseInt(ja.get(i).toString()));
            JSONArray ja2 = JSONArray.fromObject(tbShoppingCart.getSelected());
            for (int k = 0; k < ja2.size(); k++) {
                JSONObject jo = JSONObject.fromObject(ja2.get(k));
                TbProperty tbProperty = tbPropertyService.queryObject(Integer.parseInt(jo.getString("id")));
                TbCommodityProperty tbCommodityProperty = tbCommodityPropertyService.queryCidAndPid(tbShoppingCart.getCommodityid(),tbProperty.getId());
                if (tbCommodityProperty==null){
                    return R.error(tbShoppingCart.getCommodityname()+"该商品已失效,请重新添加");
                }
                if (tbShoppingCart.getCommoditynumber() > tbCommodityProperty.getPropertynum()) {
                    return R.error("库存不足！");
                }
            }

            money += tbShoppingCart.getPrice() * tbShoppingCart.getCommoditynumber();

        }
        if (money <= tbUser.getAgentinventory()) {
            tbOutGoos.setAddress(addressid);
            tbOutGoos.setShoppingid(shoppingid);
            tbOutGoos.setRemark(remark);
            tbOutGoos.setPhone(phone);
            tbOutGoos.setIdcard(idcard);
            tbOutGoos.setPhone2(phone2);
            tbOutGoos.setCreatedate(new Date());
            tbOutGoos.setUserid(tbUser.getUserId());
            tbOutGoos.setName(tbUser.getUsername());
            tbOutGoos.setMoney((int) money);
            tbOutGoos.setState(1);
            tbOutGoosService.save(tbOutGoos);
            tbUser.setAgentinventory((int) (tbUser.getAgentinventory() - money));
            tbUserService.update(tbUser);
            for (int q = 0;q<ja.size();q++){
                TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(Integer.parseInt(ja.get(q).toString()));
                tbShoppingCart.setStatus(true);
                tbShoppingCartServices.updateByPrimaryKey(tbShoppingCart);
            }
            return R.ok();
        } else {
            return R.error().put("msg", "仓库库存不足");
        }
    }

    /**
     * 改变进货单张泰
     *
     * @return
     */
    @RequestMapping("/findbyoutgoodsbyid")
    public R findbyoutgoodsbyid(@LoginUserApi TbUser tbUser, int id) {
        TbOutGoos tbOutGoos = tbOutGoosService.queryObject(id);
        tbOutGoos.setState(3);
        tbOutGoosService.update(tbOutGoos);
        return R.ok();
    }


    /**
     * 根据用户ID查询代理进货单
     *
     * @return
     */
    @RequestMapping("/foundByUserId")
    public R foundByUserId(@LoginUserApi TbUser tbUser, @RequestParam Map<String, Object> params) {
        params.put("userid",tbUser.getUserId());
        Query query = new Query(params);
        List<TbOutGoos> list = tbOutGoosService.queryList(query);
        int total = tbOutGoosService.queryTotal(query);
        List<outgoosDto> dtoList = new ArrayList<>();
        for (TbOutGoos outgoos:list) {
            outgoosDto dto = new outgoosDto();
            dto.setTbOutGoos(outgoos);
            JSONArray ja = JSONArray.fromObject(outgoos.getShoppingid());
            List<TbShoppingCart> shoppingCartList = new ArrayList<>();
            for (int i =0 ;i<ja.size();i++){
                TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(Integer.parseInt(ja.get(i).toString()));
                shoppingCartList.add(tbShoppingCart);
            }
            dto.setTbShoppingCart(shoppingCartList);
            dtoList.add(dto);
        }
        PageUtils pageUtil = new PageUtils(dtoList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    class outgoosDto{
        private TbOutGoos tbOutGoos;
        private List<TbShoppingCart> tbShoppingCart;

        public TbOutGoos getTbOutGoos() {
            return tbOutGoos;
        }

        public void setTbOutGoos(TbOutGoos tbOutGoos) {
            this.tbOutGoos = tbOutGoos;
        }

        public List<TbShoppingCart> getTbShoppingCart() {
            return tbShoppingCart;
        }

        public void setTbShoppingCart(List<TbShoppingCart> tbShoppingCart) {
            this.tbShoppingCart = tbShoppingCart;
        }
    }
}
